﻿using DFGK.IService;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.PC.基础数据
{
    /// <summary>
    /// 供应商
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class SupplierController : BaseController
    {
        private readonly ISupplierService supplierService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="supplierService"></param>
        public SupplierController(IWebHostEnvironment environment, ISupplierService supplierService) : base(environment)
        {
            this.supplierService = supplierService;
        }

        /// <summary>
        /// 查询供应商
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllSupplier(string? name, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = supplierService.GetAllSupplier(name, pageIndex, pageSize, out var totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 根据供应商id查看供应商
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSupplierById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = supplierService.GetSupplierById(id);
            if(res!=null)
                return Ok(BaseResult.Result(res, "200",0));
            return Ok(BaseResult.Result(null, "200",0));
        }

        /// <summary>
        /// 添加供应商
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddSupplier(Supplier supplier)
        {
            if (string.IsNullOrEmpty(supplier.Code) || supplier.Code.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }       
            supplier.CreateUserId = base.BaseGetNowUserId();
            supplier.CreateUserName = base.BaseGetNowUserName();
            string message = "";
            var res = supplierService.AddSupplier(supplier, out message);
            if (res)
            {          
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }
        /// <summary>
        /// 修改供应商
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdSupplier(Supplier supplier)
        {
            if (supplier.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(supplier.Code) || supplier.Code.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }
            supplier.ModifyUserId = base.BaseGetNowUserId() ;
            supplier.ModifyUserName = base.BaseGetNowUserName();
            string message = "";
            var res = supplierService.UpdSupplier(supplier, out message);
            if (res)
            {
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！" + message, "500"));
        }
        /// <summary>
        /// 删除供应商
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelSUpplier(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = supplierService.DelSupplier(id);
            if (res)
            {        
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }
        /// <summary>
        /// 批量删除供应商
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelSUpplierList(long[] idList)
        {
            if (idList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var daRes = true;
            foreach (var id in idList)
            {
                var res = supplierService.DelSupplier(id);
                if (!res)
                {
                    daRes = false;
                    break;
                }
            }
            if (daRes)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }
    }
}
